package com.olc.railing.ui;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;

import com.olc.railing.listener.OnGpio0ValueListener;
import com.olc.railing.listener.OnGpio1ValueListener;
import com.softwinner.Gpio;
import com.olc.railing.utils.MyLog;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.functions.Predicate;
import io.reactivex.schedulers.Schedulers;


public class GpioService extends Service {
	private static final String TAG = "GpioService";
	private OnGpio0ValueListener onGpio0ValueListener;
	private OnGpio1ValueListener onGpio1ValueListener;
	private Disposable disposable;
	@Override
	public IBinder onBind(Intent arg0) {
		Log.d(TAG,"onBind");
		//initGpio();
		startTimer();

		return new GpioBinder();
	}

	public void setAgain(boolean again){
		this.again = again;
	}



	public class GpioBinder extends Binder{
		public GpioService getService(){
			return GpioService.this;
		}

	}

	private boolean again;
	private boolean isFrist;

	private boolean isFrist2;
	/**
	 * 开始定时执行
	 */
	private void startTimer() {
		Observable observable = Observable.interval( 1000, 50, TimeUnit.MILLISECONDS)
				.observeOn(Schedulers.io())
				.flatMap(new Function<Long, ObservableSource<Integer>>() {
					@Override
					public ObservableSource<Integer> apply(Long aLong) throws Exception {
						return Observable.just(Gpio.readGpio('O',3));
					}
				});
		observable.subscribe(new Observer<Integer>() {
			@Override
			public void onSubscribe(@NonNull Disposable d) {

			}

			@Override
			public void onNext(@NonNull Integer value) {
				//MyLog.d(TAG,"gpio3 : "+value);
				if(value == 1){
					isFrist = true;
					return;
				}
				if(isFrist){
					isFrist = false;
					if(!again){
						again = true;
						if(onGpio0ValueListener!=null){
							onGpio0ValueListener.setGpioValue0();
						}
					}else{
						again = false;
						Gpio.writeGpio('O',1,0);
						if(onGpio0ValueListener!=null){
							onGpio0ValueListener.closeRecognise();
						}
					}
				}
			}

			@Override
			public void onError(@NonNull Throwable e) {

			}

			@Override
			public void onComplete() {

			}
		});

		Observable observable1 = Observable.interval( 1000, 50, TimeUnit.MILLISECONDS)
				.observeOn(Schedulers.io())
				.flatMap(new Function<Long, ObservableSource<Integer>>() {
					@Override
					public ObservableSource<Integer> apply(Long aLong) throws Exception {
						return Observable.just( Gpio.readGpio('O',4));
					}
				});
		observable1.subscribe(new Observer<Integer>() {
			@Override
			public void onSubscribe(@NonNull Disposable d) {

			}

			@Override
			public void onNext(@NonNull Integer value) {
				//MyLog.d(TAG,"gpio4 : "+value);
				if(value == 1){
					isFrist2 = true;
					return;
				}
				if(isFrist2){
					isFrist2 = false;
					Gpio.writeGpio('O',2,1);
					if(onGpio1ValueListener!=null){
						onGpio1ValueListener.setGpioValue1();
					}
				}
			}

			@Override
			public void onError(@NonNull Throwable e) {

			}

			@Override
			public void onComplete() {

			}
		});
	}





	// 设置回调接口
	public void setOnGpio0ValueListener(OnGpio0ValueListener onGpio0ValueListener){
		this.onGpio0ValueListener = onGpio0ValueListener;
	}
	// 设置回调接口
	public void setOnGpio1ValueListener(OnGpio1ValueListener onGpio1ValueListener){
		this.onGpio1ValueListener = onGpio1ValueListener;
	}
}
